import { http } from '@/utils/request'
import type { ApiResponse, PaginationResult, Question } from '@/types'

// 收藏项类型定义
export interface FavoriteItem {
  id: number
  type: 'question' | 'resource' | 'contest'
  title: string
  description?: string
  difficulty?: 'Easy' | 'Medium' | 'Hard'
  favorited_at: string
  question?: Question
  resource?: any
  contest?: any
}

// 收藏列表响应类型
export interface FavoriteListResponse {
  items: FavoriteItem[]
  total: number
  page: number
  page_size: number
  total_pages: number
}

// 收藏筛选参数
export interface FavoriteFilters {
  type?: 'question' | 'resource' | 'contest'
  difficulty?: 'Easy' | 'Medium' | 'Hard'
  search?: string
  page?: number
  page_size?: number
}

/**
 * 收藏相关API
 */
export const favoriteApi = {
  // 获取用户收藏列表
  getFavorites(params: FavoriteFilters = {}): Promise<ApiResponse<FavoriteListResponse>> {
    return http.get('/v1/users/favorites', { params })
  },

  // 获取收藏的题目列表
  getFavoriteQuestions(params: {
    difficulty?: string
    search?: string
    page?: number
    page_size?: number
  } = {}): Promise<ApiResponse<{
    items: Question[]
    total: number
    page: number
    page_size: number
    total_pages: number
  }>> {
    return http.get('/v1/questions/favorites', { params })
  },

  // 切换收藏状态
  toggleFavorite(questionId: number): Promise<ApiResponse<{
    message: string
    is_favorited: boolean
  }>> {
    return http.post(`/v1/users/${questionId}/favorite`)
  },

  // 取消收藏
  removeFavorite(favoriteId: number): Promise<ApiResponse<{
    message: string
  }>> {
    return http.delete(`/v1/favorites/${favoriteId}`)
  },

  // 批量取消收藏
  removeFavorites(favoriteIds: number[]): Promise<ApiResponse<{
    message: string
    removed_count: number
  }>> {
    return http.delete('/v1/favorites/batch', {
      data: { favorite_ids: favoriteIds }
    })
  }
}
